/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.cadmium;

import java.applet.Applet;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;

/**
 * This class provides primitives related to applet handling.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
@PrimitiveProvider
public final class Applets {

    /**
     * No instance of this class.
     */
    private Applets() {
    } // end empty constructor

    /**
     * Returns the value of an applet named parameter.
     * @param ctxt context
     * @param obj object representing the applet
     * @param name parameter name
     * @return the value of the name parameter, <tt>null</tt> if not set
     * @throws Fail.Exception if passed object is not a non-null applet
     */
    @Primitive
    public static Value cadmium_applet_get_parameter(final CodeRunner ctxt,
                                                     final Value obj,
                                                     final Value name)
        throws Fail.Exception {
        try {
            final Applet applet = (Applet) obj.asBlock().asCustom();
            final String res = applet.getParameter(name.asBlock().asString());
            if (res != null) {
                return Value.createFromBlock(Block.createString(res));
            } else {
                Fail.raiseNotFound();
                return Value.UNIT; // never reached
            } // end if/else
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmium_applet_get_parameter(CodeRunner, Value, Value)'

} // end class 'Applets'
